package com.youxuit.youxu.kecheng.threadLocal.usage1;

import java.text.SimpleDateFormat;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池实现 日期转换工具类(多个任务访问 )
 */
public class DateFormatUsage05 {

    static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            int finalI = i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    SimpleDateFormat simpleDateFormat = SimpleDateFormatHolder.holder.get();
                    System.out.println(simpleDateFormat.format(finalI * 1000  ));
                }
            });
        }
        executorService.shutdown();
    }
}

class SimpleDateFormatHolder {
    static ThreadLocal<SimpleDateFormat> holder = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        }
    };
    static ThreadLocal<SimpleDateFormat> holder2 =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));


}
